package com.cote.douhahou.pretloan.utils;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cote.douhahou.pretloan.R;


public class KindTipAkomapalendDialog extends Dialog {

    private final String content;
    private final String cancelBtnText, okBtnText;
    private final Runnable runOnDeny, runOnAgree;

    public KindTipAkomapalendDialog(
            Context context, String content, String cancelBtnText, String okBtnText, Runnable runOnDeny, Runnable runOnAgree) {
        super(context);
        this.content = content;
        this.cancelBtnText = cancelBtnText;
        this.okBtnText = okBtnText;
        this.runOnDeny = runOnDeny;
        this.runOnAgree = runOnAgree;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_tip);

        getWindow().setBackgroundDrawableResource(R.color.transparent);
        setCanceledOnTouchOutside(true);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        Akoinit();
    }

    @SuppressLint({"SetTextI18n", "ClickableViewAccessibility"})
    private void Akoinit(){


        TextView contentTv = findViewById(R.id.dkta_tv_text);
        TextView cancelBtn = findViewById(R.id.dkta_btn_cancel);
        TextView agreeBtn = findViewById(R.id.dkta_btn_ok);


        if (!TextUtils.isEmpty(content)) contentTv.setText(kindtipTest(content));
        if (!TextUtils.isEmpty(cancelBtnText)) cancelBtn.setText(kindtipTest(cancelBtnText));
        if (!TextUtils.isEmpty(okBtnText)) agreeBtn.setText(kindtipTest(okBtnText));

        if (runOnDeny == null) cancelBtn.setVisibility(View.GONE);
        else
            cancelBtn.setOnClickListener(v -> {
                dismiss();
                runOnDeny.run();
            });

        agreeBtn.setOnClickListener(v -> {
            dismiss();
            if (runOnAgree !=null) runOnAgree.run();
        });

        findViewById(R.id.dkta_view_out).setOnClickListener(v -> dismiss());

        findViewById(R.id.dkta_view_in).setOnTouchListener((v, event) -> true);

    }

    private String kindtipTest(String content){

        return content.trim();

    }


}
